<xsl:stylesheet version="1.0"
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:rwt="http://schema.pbs.gov.au/RWT/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

  <db:article>
    <db:info>
      <db:title>Cost Efficient Vial Combination</db:title>
      <db:subtitle>Pre-Processing</db:subtitle>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2013-04-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version, derived from vial-combo-part1.xsl.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet prepares pricing data for the calculation of the most cost efficient vial combination.</db:para>
    <db:para>The source document is the PBS XML v2.6 (or later).</db:para>
    <db:para>The result document is a modified PBS XML description of the listing.</db:para>
    <db:note>
      <db:para>In the stylesheet comments, PUOMP is short for "Per Unit-Of-Measure Price".</db:para>
    </db:note>
  </db:article>

  <xsl:output method="xml" indent='yes'/>
  <xsl:strip-space elements='*'/>

  <db:param>The PBS item to be dispensed.</db:param>
  <xsl:param name='item-code'/>

  <db:param>Dispensary type.</db:param>
  <xsl:param name='dispensing-rule'
    select='"http://schema.pbs.gov.au/DR/IN/s90-cp"'/>
  <xsl:variable name='dr.qname'>
    <xsl:call-template name='abbreviate-uri'>
      <xsl:with-param name='uri' select='$dispensing-rule'/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:key name='pr'
    match='pbs:prescribing-rule'
    use='pbs:code'/>

  <xsl:template match='/'>
    <xsl:if test='not(key("pr", $item-code)/pbs:infusible)'>
      <xsl:message terminate='yes'>Item code "<xsl:value-of select='$item-code'/>" must refer to an infusible prescribing rule</xsl:message>
    </xsl:if>

    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match='pbs:root'>
    <xsl:copy>
      <xsl:apply-templates select='@*|*[not(self::pbs:changes-list |
                                   self::pbs:groups-list |
                                   self::rwt:restrictions-list |
                                   self::rdf:RDF)]'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='pbs:schedule'>
    <xsl:copy>
      <xsl:apply-templates select='@*|pbs:copayments-list'/>
      <xsl:apply-templates select='pbs:listings-list[pbs:prescribing-rule/pbs:code = $item-code]'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='pbs:listings-list'>
    <xsl:copy>
      <xsl:apply-templates select='@*|*[not(self::pbs:prescribing-rule)]'/>
      <xsl:apply-templates select='pbs:prescribing-rule[pbs:code = $item-code]'/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="pbs:dispensing-rule">
    <xsl:if test="@about = $dr.qname">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match='pbs:tpuu-pharmacy-price/pbs:price'>
    <xsl:if test="@dispensing-rule = $dr.qname">
      <xsl:copy>
        <xsl:apply-templates select='@*'/>
        <xsl:apply-templates select='node()'/>
        <pbs:puomp>
          <xsl:value-of select="format-number(pbs:amount div ../../pbs:vial-content, '0.00000000')"/>
        </pbs:puomp>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match='pbs:drugs-list'>
    <xsl:copy>
      <xsl:apply-templates select='@* |
                                   pbs:mp[@xml:id = translate(key("pr", $item-code)/pbs:infusible/pbs:mp-reference/@xlink:href, "#", "")]'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name='copy' match="*|@*">
    <xsl:copy>
      <xsl:apply-templates select='@*|node()'/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name='resolve-qname'>
    <xsl:param name='qname'/>

    <xsl:variable name='prefix' select='substring-before($qname, ":")'/>
    <xsl:variable name='local-name' select='substring-after($qname, ":")'/>

    <xsl:choose>
      <xsl:when test='starts-with($qname, "http:") or
                      starts-with($qname, "urn:")'>
        <!-- already resolved -->
        <xsl:value-of select='$qname'/>
      </xsl:when>
      <xsl:when test='contains($qname, ":")'>
        <xsl:value-of select='/namespace::*[name() = $prefix]'/>
        <xsl:value-of select='$local-name'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select='$qname'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name='abbreviate-uri'>
    <xsl:param name='uri'/>

    <xsl:for-each select='/*/namespace::*'>
      <xsl:if test='starts-with($uri, .) and
                    name() != "pbs"'>
        <xsl:value-of select='name()'/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select='substring-after($uri, .)'/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
